/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.desmond.squarecamera.slice;

import com.desmond.squarecamera.ImageParameters;
import com.desmond.squarecamera.ImageUtility;
import com.desmond.squarecamera.ResourceTable;
import com.desmond.squarecamera.util.BitmapUtil;
import com.desmond.squarecamera.util.CameraConstants;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.utils.Matrix;
import ohos.media.image.PixelMap;
import ohos.utils.net.Uri;

public class EditSavePhotoSlice extends AbilitySlice {

    public static final String TAG = EditSavePhotoSlice.class.getSimpleName();

    public static final String BITMAP_KEY = "bitmap_byte_array";
    public static final String ROTATION_KEY = "rotation";
    public static final String IMAGE_INFO = "image_info";

    private static final int REQUEST_STORAGE = 1;

    private Image photoImageView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_edit_save_photo);

        int rotation = intent.getIntParam(ROTATION_KEY, 0);
        byte[] data = intent.getByteArrayParam(BITMAP_KEY);
        ImageParameters imageParameters = intent.getSequenceableParam(IMAGE_INFO);

        photoImageView = (Image) findComponentById(ResourceTable.Id_photo);
        rotatePicture(rotation, data, photoImageView);

        findComponentById(ResourceTable.Id_save_photo)
                .setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                savePicture();
            }
        });

        findComponentById(ResourceTable.Id_cancel)
                .setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
    }

    private void rotatePicture(int rotation, byte[] data, Image photoImageView) {
        PixelMap pixelMap = ImageUtility.decodeSampledBitmapFromByte(getContext(), data);
        if (rotation != 0) {
            PixelMap oldPixelMap = pixelMap;

            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);

            pixelMap = BitmapUtil.createPixelMap(oldPixelMap,
                    0,
                    0,
                    oldPixelMap.getImageInfo().size.width,
                    oldPixelMap.getImageInfo().size.height,
                    matrix,
                    false);

            oldPixelMap.release();
        }

        photoImageView.setPixelMap(pixelMap);
    }

    private void savePicture() {

        PixelMap pixelMap = photoImageView.getPixelMap();

        Uri uri = ImageUtility.savePicture(getContext(), pixelMap);

        Intent intent = new Intent();
        intent.setUri(uri);

        getAbility().setResult(CameraConstants.CAMERA_RESULT_CODE, intent);
        getAbility().terminateAbility();
    }

}
